package com.a1anwang.okble.demo.views;


import com.a1anwang.okble.common.OKBLEDataUtils;
import com.a1anwang.okble.demo.ResourceTable;

import com.a1anwang.okble.demo.utils.ToastUtil;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.UUID;

public class CompleteUUIDInputPopupWindow implements Component.ClickedListener {
    CommonDialog popWindow = null;
    TextField tvValue;
    int screenWidht;
    int screenHeight;
    private Context mContext;
    private InputListener inputListener;
    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public CompleteUUIDInputPopupWindow(Context context) {
        this.mContext = context;
        screenWidht = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes().width;
        screenHeight = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes().height;
        popWindow = new CommonDialog(mContext);
        Component layout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_service_uuid_input, null, true);
        popWindow.setAlignment(TableLayout.Alignment.ALIGNMENT_BOTTOM);
        popWindow.setTransparent(true);
        popWindow.setContentCustomComponent(layout);
        popWindow.setSize(screenWidht,screenHeight);
        init(layout);
    }

    public void setInputListener(InputListener inputListener) {
        this.inputListener = inputListener;
    }

    private void init(Component layout) {
        tvValue = (TextField) layout.findComponentById(ResourceTable.Id_tv_value);
        layout.findComponentById(ResourceTable.Id_btn_0).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_1).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_2).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_3).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_4).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_5).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_6).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_7).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_8).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_9).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_a).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_b).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_c).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_d).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_e).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_f).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_x).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_done).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_back).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_line).setClickedListener(this);
        layout.findComponentById(ResourceTable.Id_btn_random).setClickedListener(this);
    }
    /**
     * 展示 PopWindow
     */

    public void showAt() {
        popWindow.show();
    }
    /**
     * 隐藏 PopWindow
     */
    public void dismiss() {
        if (popWindow != null) {
            popWindow.destroy();
        }
    }
    /**
     * onClick处理点击事件
     *
     * @param component 控件对象
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_0:
                tvValue.append("0");
                break;
            case ResourceTable.Id_btn_1:
                tvValue.append("1");
                break;
            case ResourceTable.Id_btn_2:
                tvValue.append("2");
                break;
            case ResourceTable.Id_btn_3:
                tvValue.append("3");
                break;
            case ResourceTable.Id_btn_4:
                tvValue.append("4");
                break;
            case ResourceTable.Id_btn_5:
                tvValue.append("5");
                break;
            case ResourceTable.Id_btn_6:
                tvValue.append("6");
                break;
            case ResourceTable.Id_btn_7:
                tvValue.append("7");
                break;
            case ResourceTable.Id_btn_8:
                tvValue.append("8");
                break;
            case ResourceTable.Id_btn_9:
                tvValue.append("9");
                break;
            case ResourceTable.Id_btn_a:
                tvValue.append("A");
                break;
            case ResourceTable.Id_btn_b:
                tvValue.append("B");
                break;
            case ResourceTable.Id_btn_c:
                tvValue.append("C");
                break;
            case ResourceTable.Id_btn_d:
                tvValue.append("D");
                break;
            case ResourceTable.Id_btn_e:
                tvValue.append("E");
                break;
            case ResourceTable.Id_btn_f:
                tvValue.append("F");
                break;
            case ResourceTable.Id_btn_line:
                tvValue.append("-");
                break;
            case ResourceTable.Id_btn_x:
                String nowStr = tvValue.getText().toString();
                if (nowStr.length() > 0) {
                    tvValue.setText(nowStr.substring(0,nowStr.length() - 1));
                }
                break;
            case ResourceTable.Id_btn_done:
                String value = tvValue.getText().toString();
                if (!OKBLEDataUtils.isValidUUID(value)) {
                    ToastUtil.toast(mContext,"uuid is not valid");
                    return;
                }
                if (inputListener != null) {
                    inputListener.onInputComplete(value);
                }
                tvValue.setText("");
                popWindow.destroy();
                break;
            case ResourceTable.Id_btn_back:
                tvValue.setText("");
                popWindow.destroy();
                break;
            case ResourceTable.Id_btn_random:
                tvValue.setText(UUID.randomUUID().toString().toUpperCase());
                break;
            default:
                break;
        }
    }

    /**
     * InputListener 接口
     *
     * @since 2021-05-14
     */
    public interface InputListener {

        /**
         * onInputComplete设置value
         *
         * @param value 字符串
         */
        void onInputComplete(String value);
    }
}
